package com.hzhc.controller.pad;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.CntrInfo;
import com.hzhc.entity.YwOrderCntr;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.CntrInfoService;
import com.hzhc.service.DeclareCarService;
import com.hzhc.service.VehicleReservationService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.*;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @ClassName: LoginController
 * @Description:
 * @version 1.0
 * @author: 功不唐捐
 * @Date: 2024-06-03
 */
@RestController
@RequestMapping("/ipad")
@RequiredArgsConstructor
public class IpadCntrTallyController {

	private final CntrInfoService cntrService;
	private final DeclareCarService declareCarService;
	private final VehicleReservationService vehicleReservationService;



	/**
	 * 集装箱落场理货
	 *
	 * @param request
	 * @param
	 * @return
	 */
	@PostMapping("/cntrDuiTally")
	@SysTemLog(operModul = "落场理货", operType = "落场理货", operDesc = "落场理货功能", modular = "IPAD")
	public ResultUtil<T> cntrDuiTally(HttpServletRequest request, @RequestBody String json) {
		JSONObject object = JSONObject.parseObject(json);
		CntrTallyVo cntrTallyVo = new CntrTallyVo();
		String deviceNo = request.getHeader("deviceNo");// 设备号
		if (StrUtil.isNotBlank(deviceNo)) {
			cntrTallyVo.setDeviceNo(deviceNo);
		}
		if (StrUtil.isNotBlank(object.getString("cntrNo"))) {
			cntrTallyVo.setCntrNo(object.getString("cntrNo"));
		}
		if (StrUtil.isNotBlank(object.getString("bay"))) {
			cntrTallyVo.setBay(object.getString("bay"));
		}
		if (StrUtil.isNotBlank(object.getString("jhBay"))) {
			cntrTallyVo.setJhBay(object.getString("jhBay"));
		}
		if (StrUtil.isNotBlank(object.getString("id"))) {
			cntrTallyVo.setId(object.getLong("id"));
		}

		if (StrUtil.isNotBlank(object.getString("userId"))) {
			cntrTallyVo.setUid(object.getLong("userId"));
		}
		if (StrUtil.isNotBlank(object.getString("declareId"))) {
			cntrTallyVo.setDeclareId(object.getLong("declareId"));
		}
		if (StrUtil.isNotBlank(object.getString("machineCode"))) {
			cntrTallyVo.setMachineCode(object.getString("machineCode"));
		}
		if (StrUtil.isNotBlank(object.getString("machineName"))) {
			cntrTallyVo.setMachineName(object.getString("machineName"));
		}
		if (StrUtil.isNotBlank(object.getString("driverName"))) {
			cntrTallyVo.setDriverName(object.getString("driverName"));
		}
		if (StrUtil.isNotBlank(object.getString("carNo"))) {
			cntrTallyVo.setCarNo(object.getString("carNo"));
		}
		if (object.getLong("vehicleId")!=null) {
			cntrTallyVo.setVehicleId(object.getLong("vehicleId"));
		}
		if (StrUtil.isNotBlank(object.getString("partialLoadNo"))) {
			cntrTallyVo.setPartialLoadNo(object.getString("partialLoadNo"));
		}
		if (StrUtil.isNotBlank(object.getString("lhWeight"))) {
			cntrTallyVo.setLhWeight(object.getString("lhWeight"));
		}
		if (StrUtil.isNotBlank(object.getString("efFlag"))) {
			cntrTallyVo.setEfFlag(object.getString("efFlag"));
		}
		UserDto user = TokenUtil.getUser(request);
		if(user!=null){
			cntrTallyVo.setUid(user.getId());
			cntrTallyVo.setUserName(user.getRealname());
		}
		cntrTallyVo.setAccesspath("平板端");
		return 	cntrService.cntrDuiTally(cntrTallyVo);
	}


	/**
	 * 集装箱移箱
	 *
	 * @param request
	 * @param
	 * @return
	 */
	@PostMapping("/cntrMoveBay")
	@SysTemLog(operModul = "移箱", operType = "移箱", operDesc = "移箱功能", modular =
			"IPAD")
	public ResultUtil<T> cntrMoveBay(HttpServletRequest request, @RequestBody CntrTallyVo cntrTallyVo) {
		// 设备号
		String deviceNo = request.getHeader("deviceNo");
		cntrTallyVo.setAccesspath("平板端");
		cntrTallyVo.setDeviceNo(deviceNo);
		return cntrService.cntrMoveBay(cntrTallyVo);
	}


	/**
	 * 集装箱提箱理货
	 *
	 * @param request
	 * @param
	 * @return
	 */
	@PostMapping("/cntrTxTally")
	@SysTemLog(operModul = "提箱理货", operType = "提箱理货", operDesc = "提箱理货功能", modular = "IPAD")
	public ResultUtil<T> cntrTxTally(HttpServletRequest request, @RequestBody DeclareCarVo declareCarVo) {
		declareCarVo.setAccesspath("平板端");
		return declareCarService.updateCntrTxTallyNew(declareCarVo);
	}

	/**
	 * 根据委托号 获取提箱的箱子
	 */
	@RequestMapping("/getCntrByOrderNo")
	public ResultUtil<Map> getCntrByOrderNo(HttpServletRequest request, @RequestBody YwOrderCntrVo vo) {
		Map ywOrderCntrs = declareCarService.getCntrByOrderNo(vo);
		return ResultUtil.build(ywOrderCntrs, ResultUtilCodeEnum.SUCCESS);
	}

	/**
	 * 获取提箱的箱子
	 */
	@RequestMapping("/getCntr")
	public ResultUtil<List<YwOrderCntrVo>> getCntr(HttpServletRequest request, @RequestBody YwOrderCntrVo vo) {
		List<YwOrderCntrVo> ywOrderCntrs = declareCarService.getCntr(vo);
		return ResultUtil.build(ywOrderCntrs, ResultUtilCodeEnum.SUCCESS);
	}

	/**
	 * 获取特殊区域的箱子
	 */
	@RequestMapping("/getCntrB")
	public ResultUtil<List<CntrInfoVo>> getCntr(HttpServletRequest request, @RequestBody CntrInfoVo vo) {
		List<CntrInfoVo> cntrList = cntrService.getCntrB(vo);
		return ResultUtil.build(cntrList, ResultUtilCodeEnum.SUCCESS);
	}

	/**
	 * 中控理货 车辆预约数据
	 */
	@RequestMapping("/getVehicleList")
	public ResultUtil<List<VehicleReservationVo>> getVehicleList(HttpServletRequest request,@RequestBody VehicleReservationVo vo) {
		Page<VehicleReservationVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
		UserDto user = TokenUtil.getUser(request);
		List<VehicleReservationVo> vehicleReservationVos = vehicleReservationService.getVehicleList(page, vo);
		return ResultUtil.build(vehicleReservationVos,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
	}

	/**
	 * 提箱退箱理货
	 *
	 * @param request
	 * @param
	 * @return
	 */
	@PostMapping("/backCntrTally")
	@SysTemLog(operModul = "提箱退箱理货", operType = "提箱退箱", operDesc = "提箱退箱理货功能", modular = "IPAD")
	public ResultUtil<T> backCntrTally(HttpServletRequest request, @RequestBody DeclareCarVo declareCarVo) {
		declareCarVo.setAccesspath("平板端");
		return declareCarService.backCntrTally(declareCarVo);
	}

	/**
	 * 取消提箱
	 *
	 * @param request
	 * @param
	 * @return
	 */
	@PostMapping("/cancelTxRet")
	@SysTemLog(operModul = "提箱取消提箱", operType = "取消提箱", operDesc = "提箱取消提箱功能", modular = "IPAD")
	public ResultUtil<T> cancelTxRet(HttpServletRequest request, @RequestBody DeclareCarVo declareCarVo) {
		declareCarVo.setAccesspath("平板端");
		return declareCarService.cancelTxRet(declareCarVo);
	}

	/**
	 * 修改箱重量
	 *
	 * @param request
	 * @param
	 * @return
	 */
	@PostMapping("/changeLhWeight")
	@SysTemLog(operModul = "箱重量修改", operType = "箱重量修改", operDesc = "箱重量修改功能", modular = "IPAD")
	public ResultUtil<T> changeLhWeight(HttpServletRequest request, @RequestBody CntrInfoVo cntrInfoVo) {
		return cntrService.changeLhWeight(cntrInfoVo);
	}

	/**
	 * 提箱新增集装箱
	 *
	 * @param request
	 * @param
	 * @return
	 */
	@PostMapping("/addTxCntr")
	@SysTemLog(operModul = "提箱新增集装箱", operType = "新增集装箱", operDesc = "提箱新增集装箱功能", modular = "IPAD")
	public ResultUtil<T> addTxCntr(HttpServletRequest request, @RequestBody CntrInfoVo cntrInfoVo) {
		return cntrService.addTxCntr(cntrInfoVo);
	}
}
